package com.chushouya.order.dao.repository;

import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.order.dao.entity.StatClerkOrderDayEntity;
import com.chushouya.order.dao.mapper.StatClerkOrderDayEntityMapper;
import com.chushouya.order.dto.api.clerk.ClerkOrderDataDTO;
import com.chushouya.order.dto.api.stat.ClerkOrderDayStatQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Repository
public class StatClerkOrderDayRepository extends BaseRepository<StatClerkOrderDayEntityMapper, StatClerkOrderDayEntity, Long> {

    public StatClerkOrderDayEntity selectOne(ClerkOrderDayStatQuery query) {
        return this.selectFirstOneByExample(this.buildExample(query));
    }

    public List<StatClerkOrderDayEntity> selectList(ClerkOrderDayStatQuery query) {
        return this.selectByExample(this.buildExample(query));
    }

    private Example buildExample(ClerkOrderDayStatQuery query) {
        final Example example = this.buildExample();
        if (Objects.nonNull(query.getClerkId())) {
            example.and().andEqualTo("clerkId", query.getClerkId());
        }
        if (Objects.nonNull(query.getStatDate())) {
            example.and().andEqualTo("statDate", query.getStatDate());
        }
        if (Objects.nonNull(query.getStatDateStart()) && Objects.nonNull(query.getStatDateEnd())) {
            example.and().andGreaterThanOrEqualTo("statDate", query.getStatDateStart());
            example.and().andLessThanOrEqualTo("statDate", query.getStatDateEnd());
        }
        example.orderBy("statDate").desc();
        return example;
    }

    public Integer selectDispatchCount(Long clerkId, Date startTime, Date endTime) {
        return this.mapper.selectDispatchCount(clerkId, startTime, endTime);
    }

    public Integer selectCompleteCount(Long clerkId, Date startTime, Date endTime) {
        return this.mapper.selectCompleteCount(clerkId, startTime, endTime);
    }

    public Integer selectDeliverCount(Long clerkId, Date startTime, Date endTime) {
        return this.mapper.selectDeliverCount(clerkId, startTime, endTime);
    }

    public Integer selectStockCount(Long clerkId, Date startTime, Date endTime) {
        return 0;
    }

    public Integer selectReturnedCount(Long clerkId, Date startTime, Date endTime) {
        return this.mapper.selectReturnedCount(clerkId, startTime, endTime);
    }

    public Integer selectSelfCreateCount(Long clerkId, Date startTime, Date endTime) {
        return 0;
    }

    public ClerkOrderDataDTO selectOrderCount(Long clerkId, Date startTime, Date endTime) {
        return this.mapper.selectOrderCount(clerkId, startTime, endTime);
    }

    public Integer selectCancelCount(Long clerkId, Date startTime, Date endTime) {
        return this.mapper.selectCancelCount(clerkId, startTime, endTime);
    }

    public Integer selectCloseCount(Long clerkId, Date startTime, Date endTime) {
        return this.mapper.selectCloseCount(clerkId, startTime, endTime);
    }

    public Integer selectBackoutCount(Long clerkId, Date startTime, Date endTime) {
        return this.mapper.selectBackoutCount(clerkId, startTime, endTime);
    }

    public List<StatClerkOrderDayEntity> statClerkOrderMonth(Long clerkId, Date statStart, Date statEnd) {
        return this.mapper.statClerkOrderMonth(clerkId,statStart, statEnd);
    }

    public StatClerkOrderDayEntity statClerkOrderCount(Long clerkId, Date startTime, Date endTime) {
        return this.mapper.statClerkOrderCount(clerkId,startTime, endTime);
    }

    public List<StatClerkOrderDayEntity> statClerkOrderDay(Long clerkId, Date startTime, Date endTime) {
        return this.mapper.statClerkOrderDay(clerkId, startTime, endTime);
    }

    public Integer selectMultiCount(Long clerkId, Date startTime, Date endTime) {
        return this.mapper.selectMultiCount(clerkId, startTime, endTime);
    }
}
